package 力扣日常刷题.木23一月.第12天312;

import java.util.ArrayList;

/**
 * @author 帅小伙
 * @date 2022/3/12
 * @description
 */
public class Demo54区间列表的交集 {

    public int[][] intervalIntersection(int[][]firstList,int[][] secondList) {
        ArrayList<int[]> res = new ArrayList<>();
        int a = 0,b = 0;

        while(a<firstList.length && b < secondList.length) {
            int l = Math.max(firstList[a][0],secondList[b][0]);
            int r = Math.min(firstList[a][1],secondList[b][1]);
            if(l<=r){
                res.add(new int[]{l,r});
            }
            if(firstList[a][1] < secondList[b][1]) {
                a++;
            }else{
                b++;
            }
        }
        return res.toArray(new int[res.size()][]);
    }

}
